********* TABLE A8 *********
clear all
set more off 
capture log close
 
* main path to data
global path "~/Dropbox/Paraguay/1_data/7_replication_dataverse"
capture cd "$path"	

*tables = analysis tables
global tables "$path/tables"

* load rural collective action data
use "$path/data/landinv.dta", clear

xtset district_code year
generate post = (year>=1989)

eststo clear

* Panel A: Largest Eigenvalue
foreach y in l_linv linv_b l_peasfam l_hectares {
    reghdfe `y' i.post##c.lambda_z, absorb(year district_code i.year#i.depa) cluster(district_code)
    
    * Store mean of dependent variable
    summ `y' if e(sample)==1 
    estadd scalar meanDep = r(mean)
    
    eststo eigen_`y'
}

* Panel B: Average Degree
foreach y in l_linv linv_b l_peasfam l_hectares {
    reghdfe `y' i.post##c.degree_z, absorb(year district_code i.year#i.depa) cluster(district_code)
    
    * Store mean of dependent variable
    summ `y' if e(sample)==1 
    estadd scalar meanDep = r(mean)
    
    eststo degree_`y'
}

* Panel C: Average Betweenness
foreach y in l_linv linv_b l_peasfam l_hectares {
    reghdfe `y' i.post##c.betweenness_z, absorb(year district_code i.year#i.depa) cluster(district_code)
    
    * Store mean of dependent variable
    summ `y' if e(sample)==1 
    estadd scalar meanDep = r(mean)
    
    eststo between_`y'
}

* Create table header
file open tableA8 using "$tables/tableA8.tex", write replace
file write tableA8 "\begin{tabular}{lcccc} \hline" _n
file write tableA8 "& Occup. (log) & Occup. (binary) & Peasants (log) & Hectares (log) \\" _n
file write tableA8 "& (1) & (2) & (3) & (4) \\" _n
file write tableA8 "\hline" _n
file write tableA8 "\multicolumn{5}{l}{\textit{(a) Largest Eigenvalue}} \\" _n
file close tableA8

* Append panel A results
esttab eigen_* using "$tables/tableA8.tex", append fragment nonumber label style(tex) ///
    cells(b(star fmt(%12.4fc)) se(par fmt(%12.4fc))) ///
    keep(1.post#c.lambda_z) ///
    stats(r2, fmt(%12.4fc) label("\$R^2\$")) ///
    varlabels(1.post#c.lambda_z "Democracy × Eigenvalue") ///
    collabels(none) mlabels(none) eqlabels(none) starlevels(* 0.1 ** 0.05 *** 0.01) ///
    noobs nonotes noline prehead("") posthead("") prefoot("") postfoot("")

* Add panel B header
file open tableA8 using "$tables/tableA8.tex", write append
file write tableA8 "\hline" _n
file write tableA8 "\multicolumn{5}{l}{\textit{(b) Average Degree}} \\" _n
file close tableA8

* Append panel B results
esttab degree_* using "$tables/tableA8.tex", append fragment nonumber label style(tex) ///
    cells(b(star fmt(%12.4fc)) se(par fmt(%12.4fc))) ///
    keep(1.post#c.degree_z) ///
    stats(r2, fmt(%12.4fc) label("\$R^2\$")) ///
    varlabels(1.post#c.degree_z "Democracy × Degree") ///
    collabels(none) mlabels(none) eqlabels(none) starlevels(* 0.1 ** 0.05 *** 0.01) ///
    noobs nonotes noline prehead("") posthead("") prefoot("") postfoot("")

* Add panel C header
file open tableA8 using "$tables/tableA8.tex", write append
file write tableA8 "\hline" _n
file write tableA8 "\multicolumn{5}{l}{\textit{(c) Average Betweenness}} \\" _n
file close tableA8

* Append panel C results and bottom statistics
esttab between_* using "$tables/tableA8.tex", append fragment nonumber label style(tex) ///
    cells(b(star fmt(%12.4fc)) se(par fmt(%12.4fc))) ///
    keep(1.post#c.betweenness_z) ///
    stats(r2 meanDep N, fmt(%12.4fc %12.4fc %12.0fc) ///
    labels("\$R^2\$" "\hline Outcome mean" "Observations")) ///
    varlabels(1.post#c.betweenness_z "Democracy × Betweenness") ///
    collabels(none) mlabels(none) eqlabels(none) starlevels(* 0.1 ** 0.05 *** 0.01) ///
    noobs nonotes noline prehead("") posthead("") prefoot("") postfoot("\hline \end{tabular}")
